#    Tichy
#    copyright 2008 Guillaume Chereau (charlie137@gmail.com)
#
#    This file is part of Tichy.
#
#    Tichy is free software: you can redistribute it and/or modify
#    it under the terms of the GNU General Public License as published by
#    the Free Software Foundation, either version 3 of the License, or
#    (at your option) any later version.
#
#    Tichy is distributed in the hope that it will be useful,
#    but WITHOUT ANY WARRANTY; without even the implied warranty of
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#    GNU General Public License for more details.
#
#    You should have received a copy of the GNU General Public License
#    along with Tichy.  If not, see <http://www.gnu.org/licenses/>.


from widget import Widget
from geo import Vect, Rect
from sdl_painter import SdlPainter


class Screen(Widget):
    """Main widget for everything

    Screen is the widget that is at the end of the chain when we emit
    "need-redraw".  It is the only widget that start a redrawing
    sequence.  It may seem not optimized to always redraw from the top
    widget, but the idea is that you can then have transparency in any
    widgets
    """

    def __init__(self, size, fullscreen=False, **kargs):
        import tichy
        self.redraw_rect = None
        super(Screen, self).__init__(None, events_source=tichy.mainloop,
                                     **kargs)
        self.size = size
        self.painter = SdlPainter(size, fullscreen)
        self.redraw_rect = Rect(Vect(0, 0), size)

        # XXX: remove this, the event source could keep ref to the screen
        # self.monitor(tichy.mainloop, 'tick', self.on_tick)
        tichy.mainloop.screen = self

    screen = property(lambda self: self)

    def _full_draw(self):
        if not self.redraw_rect:
            return
        assert self.painter.pos.x == self.painter.pos.y == 0
        # TODO: use virtual attribute
        self.painter.set_mask(self.redraw_rect)
        # The background color
        self.painter.fill((0, 0, 0), self.size)
        super(Screen, self)._full_draw(self.painter)
        self.painter.flip(self.redraw_rect)
        self.redraw_rect = None

    def need_redraw(self, rect):
        super(Screen, self).need_redraw(rect)
        if self.redraw_rect is None:
            self.redraw_rect = rect
        else:
            self.redraw_rect = self.redraw_rect.merge(rect)

    def tick(self):
        self.update()

    def update(self):
        if self._messed:
            self._full_compute_sizes()
            self._full_organize()
        self._full_draw()

    def destroy(self):
        super(Screen, self).destroy()
        import pygame.display
        pygame.display.quit()

    def toggle_fullscreen(self):
        self.painter.toggle_fullscreen()
